package com.shuqi.activity;

import java.util.List;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.shuqi.activity.adapter.BrowsingHistoryAdapter;
import com.shuqi.activity.viewport.BottomDialog;
import com.shuqi.activity.viewport.CommonTitle;
import com.shuqi.common.Urls;
import com.shuqi.controller.R;
import com.shuqi.database.dao.impl.BookInfoDao;
import com.shuqi.database.model.BookInfo;
import com.shuqi.model.manager.MyAccountManager;

/**
 * 曾经浏览
 * 
 * @author Zhuchen
 * @date 2013-12-31
 * @version 1.0
 */
public class BrowsingHistoryActivity extends ActivityBase implements OnClickListener,
        OnItemClickListener {

    final static int MAX_BROWSINGLIST_SIZE = 30;
    private ListView browsinghistory_history_lv;
    private List<BookInfo> list;
    private BrowsingHistoryAdapter adapter;
    private BottomDialog dialog;
    private Button view_dialog_cancel;// 取消
    private Button view_dialog_ok;// 确定
    private LinearLayout act_purchasehistory_null; // 无浏览书籍
    private TextView act_purchasehistory_goshucheng;// 去书城
    private CommonTitle mTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_browsinghistory);
        activityLogicForward();
    }


    @Override
    public void activityLogicForward() {
        mTitle = (CommonTitle) findViewById(R.id.title);
        mTitle.setImageListener(this);
        mTitle.setStyle(true, true);
        browsinghistory_history_lv = (ListView) findViewById(R.id.browsinghistory_history_lv);
        act_purchasehistory_null = (LinearLayout) findViewById(R.id.act_purchasehistory_null);
        act_purchasehistory_goshucheng =
                (TextView) findViewById(R.id.act_purchasehistory_goshucheng);

        browsinghistory_history_lv.setOnItemClickListener(this);
        act_purchasehistory_goshucheng.setOnClickListener(this);
        list =
                BookInfoDao.getInstance().getBrowsingHistorys(
                        MyAccountManager.getInstance(this).getUserId());
        // 是否有浏览记录
        if (list != null && list.size() != 0) {
            adapter = new BrowsingHistoryAdapter(getApplicationContext());
            if( list.size() > MAX_BROWSINGLIST_SIZE ){
                while( list.size() > MAX_BROWSINGLIST_SIZE){
                    list.remove(list.size() - 1);
                }
            }
            adapter.setData(list);
            browsinghistory_history_lv.setAdapter(adapter);
        } else {
            act_purchasehistory_null.setVisibility(View.VISIBLE);
            browsinghistory_history_lv.setVisibility(View.GONE);
            mTitle.setStyle(true, false);

        }

    }

    @Override
    public void activityInitData() {

    }

    /**
     * 点击事件
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left:
                ActivityManager.getInstance().finishActivity(this);
                break;
            case R.id.title_right:
                View view = View.inflate(this, R.layout.view_dialog_browsinghistory, null);
                view_dialog_cancel = (Button) view.findViewById(R.id.view_dialog_cancel);
                view_dialog_ok = (Button) view.findViewById(R.id.view_dialog_ok);

                view_dialog_ok.setOnClickListener(new OnClickListener() {

                    // 确定按钮
                    @Override
                    public void onClick(View v) {
                        BookInfoDao.getInstance().delBrowsingHistory(
                                MyAccountManager.getInstance(BrowsingHistoryActivity.this)
                                        .getUserId());
                        if (adapter != null) {
                            adapter.setData(null);
                            adapter.notifyDataSetChanged();
                        }
                        showMsg("清空成功");
                        dialog.dismiss();
                        act_purchasehistory_null.setVisibility(View.VISIBLE);
                        mTitle.setStyle(true, false);
                    }
                });

                // 取消按钮
                view_dialog_cancel.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });

                if (dialog == null) {
                    dialog = new BottomDialog(this, view);
                    view.setOnClickListener(this);
                }
                dialog.show();

                break;
            // 去书城
            case R.id.act_purchasehistory_goshucheng:
//                MainActivityGroup.show(this, MainActivityGroup.PAGE_POSITINO_BOOKSQUARE, true);
                MainActivityGroup.show(this, MainActivityGroup.PAGE_POSITION_BOOKSEARCH, true);
                break;
            default:
                break;
        }

    }

    @Override
    protected void onResume() {
        // 查询数据库添加数据

        super.onResume();
    }

    /**
     * 点击条目
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BookInfo bookInfo = list.get(position);
        if (BookInfo.SOURCE_TYPE_SHEENME == bookInfo.getSourceType()) { // 如果是神马书籍跳转网页
            WebKitActivity.openWebkit(this, Urls.getWebBookCoverUrl(bookInfo.getBookId(), bookInfo.getSourceId(), this));
        } else {
            BookCoverActivity.openBookCover(this, bookInfo.getBookId());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            ActivityManager.getInstance().finishActivity(this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
